# -*- coding:utf-8 -*-
"""
the module to crawler the recently publish movie
"""
import urllib
import re

class PatternNotMatchException(Exception):
    pass

class DoubanMovieCrawlerException(Exception):
    pass

def getnewmovie():
    """
    the url is http://movie.douban.com
    the regex <ul contentlist1></ul>
    """
    theurl = "http://movie.douban.com"
    thestr = urllib.urlopen(theurl).read()
    movielist = []
    movienames = {}#used to record which movie has already be added
    for i in range(1,6):
        try:
            tmpmovielist = getnewmoiveindex(i,thestr)
            for movie in tmpmovielist:
                if movienames.has_key(movie['name']):
                    continue
                movienames[movie['name']] = 1
                movielist.append(movie)
        except PatternNotMatchException,e:
            print str(e)
    if len(movielist) == 0:
        raise DoubanMovieCrawlerException("no movie crawled")
    return movielist

def getnewmoiveindex(i,thestr):
    """
    get movie according to the index
    """
    regex = "<ul(.|\n)*?newcontent"+str(i)+"(.|\n)*?/ul>"
    searchobj =  re.search(regex,thestr)
    if searchobj:
        #print searchobj.group(0)
        ulstr = thestr[searchobj.start():searchobj.end()]
    else:
        raise PatternNotMatchException(str(i)+"pattern not match,douban movie")
    #print ulstr
    
    #extract each movie in detail
    movielist = []
    movieregex = "<li class=\"clearfix\"(.|\n)*?/li>"
    for moviemat in re.finditer(movieregex,ulstr):
        tmpdic = {}
        mstr = ulstr[moviemat.start():moviemat.end()]
        #img link
        imgregex = "<img(.|\n)*?douban\.com(.|\n)*?>"
        imgsearch = re.search(imgregex,mstr)
        if not imgsearch:
            raise PatternNotMatchException("img link match exception")
        imgstr = mstr[imgsearch.start():imgsearch.end()]
        tmpdic['imglink'] = imgstr[imgstr.index("http"):imgstr.rindex("jpg")+3]
        
        #movie name & movie id
        #nameidregex = "<a onclick=\"more(.|\n)*?/a>"
        nameidregex = "<h3>(.|\n)*?</h3>" 
        nisearch = re.search(nameidregex,mstr)
        if not nisearch:
            raise PatternNotMatchException("name and id pattern match exception")
        nistr = mstr[nisearch.start():nisearch.end()]
        #print nistr
        tmpdic['name'] = nistr[nistr.index("/\">")+3:nistr.index("</a",2)]
        tmpdic['id'] = nistr[nistr.index("/subj")+9:nistr.index("/\"")]
       
        """
        #director
        #the code here ....
        diregex = u"<span(.|\n)*?导(.|\n)*?br>".encode('utf-8')
        #print diregex
        disearch = re.search(diregex,mstr)
        if not disearch:
            raise PatternNotMatchException("direct pattern not match")
        distr = mstr[disearch.start():disearch.end()]
        tmpdic["director"] = distr[distr.index("\""),distr.rindex("\"")].strip()

        #protagonist
        prregex = "<span(.|\n)*?主(.|\n)*?br>"
        prsearch = re.search(prregex,mstr)
        if not prsearch:
            raise PatternNotMatchException("pro pattern not match")
        prstr = mstr[prsearch.start():prsearch.end()]
        tmpdic["protagonist"] = prstr[prstr.index("\""),prstr.rindex("\"")].strip()
        """
        spanregex = "<span class=\"pl\".*?<br"
        spanms = re.findall(spanregex,mstr)
        if len(spanms) != 2:
            raise PatternNotMatchException("pattern error for director and protagonist")
        distr = spanms[0]
        prstr = spanms[1]
        tmpdic["director"] = distr[distr.index("</span>")+7:distr.rindex("<br")].strip()
        tmpdic["protagonist"] = prstr[prstr.index("</span>")+7:prstr.rindex("<br")].strip()

        movielist.append(tmpdic)
    return movielist

if __name__ == "__main__":
    print getnewmovie()

